package bsuir.magistrate.test;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;

public class BinReaderDemo
{

  public static int currentPosition = -1;

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    File file = new File("21_04_2011_16_16_25_ch1_datafile.bin");
    byte[] bytes = null;
    try
    {
      bytes = getBytesOfFile(file);
    }
    catch (IOException e)
    {
      return;
    }
    showFileInformation(bytes);
  }

  private static byte[] getBytesOfFile(File file) throws IOException
  {
    byte[] bytes;

    int size = (int) file.length();//It gives the length of the file. 

    bytes = new byte[size];
    DataInputStream dis = new DataInputStream(new FileInputStream(file));
    int read = 0;
    int numRead = 0;
    while (read < bytes.length && (numRead = dis.read(bytes, read,
        bytes.length - read)) >= 0)
    {
      read = read + numRead;
    }

    // Ensure all the bytes have been read in
    if (read < bytes.length)
    {
      System.out.println("Could not completely read: " + file.getName());
    }

    return bytes;
  }

  private static void showFileInformation(byte[] bytes)
  {
    System.out.println("File signature: " + getFileSignature(bytes));
    System.out.println("Number of channels: " + getIntValue(bytes));
    System.out.println("Size of selection for one channel: " + getIntValue(bytes));
    System.out.println("Number of spectral lines: " + getIntValue(bytes));
    System.out.println("Cutoff frequency: " + getIntValue(bytes));
    System.out.println("Frequency resolution: " + getFloatValue(bytes));
    System.out.println("Data block reception time (sec): " + getFloatValue(bytes));
    System.out.println("Signal reception time (sec): " + getIntValue(bytes));
    System.out.println("Number of accepted blocks (user): " + getIntValue(bytes));
    System.out.println("The whole data size: " + getIntValue(bytes));
    System.out.println("Number of accepted blocks (system): " + getIntValue(bytes));
    System.out.println("MAX data value: " + getFloatValue(bytes));
    System.out.println("MIN data value: " + getFloatValue(bytes));
  }

  public static String getFileSignature(byte[] bytes)
  {
    return CharBuffer.wrap(
        new char[]{//
        (char) bytes[++currentPosition], //
            (char) bytes[++currentPosition], //
            (char) bytes[++currentPosition], //
            (char) bytes[++currentPosition]}).toString();
  }

  public static int getIntValue(byte[] bytes)
  {
    return ByteBuffer
        .wrap(
            new byte[]{//
            bytes[++currentPosition],//
                bytes[++currentPosition],//
                bytes[++currentPosition],//
                bytes[++currentPosition]}).order(ByteOrder.LITTLE_ENDIAN).getInt();
  }

  public static float getFloatValue(byte[] bytes)
  {
    return ByteBuffer
        .wrap(
            new byte[]{//
            bytes[++currentPosition],//
                bytes[++currentPosition],//
                bytes[++currentPosition],//
                bytes[++currentPosition]}).order(ByteOrder.LITTLE_ENDIAN).getFloat();
  }
}
